package com.realdtech.wechat.task.Service;

import com.jfinal.plugin.activerecord.Db;
import com.realdtech.wechat.task.model.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/20.
 */
public class NewsService {

    private static final NewsFinance dao=new NewsFinance().dao();//金融咨询
    private static final NewsBusiness NB=new NewsBusiness().dao();//行业资讯
    private static final NewsStrategy NS=new NewsStrategy().dao();//贷款攻略
    private static final NewsBulletin NBU=new NewsBulletin().dao();//公告栏
    private static final NewsHot NH=new NewsHot().dao();//热点资讯
    private static final NewsSlideshow NSL=new NewsSlideshow().dao();//轮播图
    public static final NewsPartnership NP = new NewsPartnership().dao();//合作商风采

    /**
     * 储存表
     * @param headline 标题
     * @param img   标题配图
     * @param contend   内容
     */
    public void add_F(String headline,String img,String contend,String author,String source){

        NewsFinance newsFinance=new NewsFinance();
        newsFinance.setHeadline(headline);
        newsFinance.setImg(img);
        newsFinance.setContent(contend);
        newsFinance.setDate(new Date());
        newsFinance.setAuthor(author);
        newsFinance.setSource(source);

        newsFinance.save();
    }
    public void add_B(String headline,String img,String contend,String author,String source){

        NewsBusiness newsBusiness=new NewsBusiness();
        newsBusiness.setHeadline(headline);
        newsBusiness.setImg(img);
        newsBusiness.setContent(contend);
        newsBusiness.setDate(new Date());
        newsBusiness.setAuthor(author);
        newsBusiness.setSource(source);

        newsBusiness.save();
    }
    public void add_S(String headline,String img,String contend,String author,String source){

        NewsStrategy newsStrategy=new NewsStrategy();
        newsStrategy.setHeadline(headline);
        newsStrategy.setImg(img);
        newsStrategy.setContent(contend);
        newsStrategy.setDate(new Date());
        newsStrategy.setAuthor(author);
        newsStrategy.setSource(source);

        newsStrategy.save();
    }
    public void add_BU(String headline,String img,String contend,String author,String source){

        NewsBulletin newsBulletin=new NewsBulletin();
        newsBulletin.setHeadline(headline);
        newsBulletin.setImg(img);
        newsBulletin.setContent(contend);
        newsBulletin.setDate(new Date());
        newsBulletin.setAuthor(author);
        newsBulletin.setSource(source);

        newsBulletin.save();
    }
    public void add_H(String headline,String contend,String author,String source){

        NewsHot newsHot=new NewsHot();
        newsHot.setHeadline(headline);
        newsHot.setContent(contend);
        newsHot.setDate(new Date());
        newsHot.setAuthor(author);
        newsHot.setSource(source);

        newsHot.save();
    }

    public void add_Partnership(String img,String title,String link){

        NewsPartnership np=new NewsPartnership();
        np.setImage(img);
        np.setTitle(title);
        np.setLink(link);
        np.setDate(new Date());
        np.save();
    }
    public void add_Slideshow(String img,String link){

        NewsSlideshow ns=new NewsSlideshow();
        ns.setImage(img);
        ns.setLink(link);
        ns.setDate(new Date());
        ns.save();
    }

    /**
     * 合作商风采
     */
    //倒序查询
    public List<NewsPartnership> find_Partnership__desc(){

        return NP.find("SELECT * FROM  news_partnership order by id desc");
    }

    //根据id查询单条数据不需循环
    public NewsPartnership find_Partnership_s(String id){

        return NP.findFirst("SELECT * FROM news_partnership WHERE id="+id+"");
    }

    //更新表 图片 图片描述 链接
    public void update_Partnership(String img,String title,String link,String id){

        Db.update("update news_partnership  set image='"+img+"',title='"+title+"',link='"+link+"' where id="+id+"");
    }

    //根据id删除
    public void delete_Partnership(String id){

        Db.update("delete FROM news_partnership WHERE id = "+id+" ");
    }

    /**
     * 轮播图
     */

    //倒序查询
    public List<NewsSlideshow> find_Slideshow__desc(){

        return NSL.find("SELECT * FROM  news_slideshow order by id desc");
    }

    //根据id查询单条数据不需循环
    public NewsSlideshow find_Slideshow_s(String id){
        return NSL.findFirst("SELECT * FROM news_slideshow WHERE id="+id+"");
    }

    //更新表 图片 链接
    public void update_Slideshow(String img,String link,String id){
        Db.update("update news_slideshow  set image='"+img+"',link='"+link+"' where id="+id+"");
    }

    //根据id删除
    public void delete_Slideshow(String id){

        Db.update("delete FROM news_slideshow WHERE id = "+id+" ");
    }

    /**
     * 查询金融咨询表
     * @return
     */
    public List<NewsFinance> findF(){

        return dao.find("SELECT * FROM news_finance order by rand() limit  6");
    }

    //根据id倒序查询所有数据
    public List<NewsFinance> findF_desc(){

        return dao.find("SELECT * FROM  news_finance order by id desc");
    }

    //根据id查询
    public NewsFinance findFid(int i){

        return dao.findFirst("SELECT * FROM news_finance WHERE id="+i+"");
    }

    //根据id查询单条数据不需循环
    public NewsFinance findFids(String id){

        return dao.findFirst("SELECT * FROM news_finance WHERE id="+id+"");
    }

    //根据id 并且 < 查询上一条，> 查询下一条
    public NewsFinance findF_paging(int id ,String size){

        return dao.findFirst("SELECT * FROM news_finance WHERE id IN ( SELECT CASE WHEN SIGN (id - "+id+") > 0 THEN MIN(id) WHEN SIGN(id - "+id+") < 0 THEN MAX(id) END AS id FROM news_finance WHERE id "+size+" "+id+" GROUP BY SIGN(id - "+id+") ORDER BY SIGN(id - "+id+") ) ORDER BY id ASC;");
    }

    //更新表 标题 头图 内容 作者 来源
    public void updateF(String headline,String img,String content,String author,String source,String id){

        Db.update("update news_finance  set headline='"+headline+"' , img='"+img+"',content='"+content+"',author='"+author+"',source='"+source+"' where id="+id+"");
    }

    /**
     * 查询行业资讯表
     * @return
     */
    public List<NewsBusiness> findB(){

        return NB.find("SELECT * FROM news_business order by rand() limit  8");
    }

    //根据id删除
    public void delete_F(String id){

        Db.update("delete FROM news_finance WHERE id = "+id+" ");
    }

    //根据id倒序查询所有数据
    public List<NewsBusiness> findB_desc(){

        return NB.find("SELECT * FROM  news_business order by id desc");
    }

    //根据id查询
    public NewsBusiness findBid(int i){

        return NB.findFirst("SELECT * FROM news_business WHERE id="+i+"");
    }

    //根据id查询单条数据不需循环
    public NewsBusiness findBids(String id){

        return NB.findFirst("SELECT * FROM news_business WHERE id="+id+"");
    }

    //根据id查询上下页
    public NewsBusiness findB_paging(int id,String size){

        return NB.findFirst("SELECT * FROM news_business WHERE id IN ( SELECT CASE WHEN SIGN (id - "+id+") > 0 THEN MIN(id) WHEN SIGN(id - "+id+") < 0 THEN MAX(id) END AS id FROM news_business WHERE id  "+size+"  "+id+" GROUP BY SIGN(id - "+id+") ORDER BY SIGN(id - "+id+") ) ORDER BY id ASC;");
    }

    //随机查询x条数据
    public List<NewsBusiness> findB_x(int x){

        return NB.find("SELECT * FROM news_business order by rand() limit "+x+"");
    }

    //更新表 标题 头图 内容 作者 来源
    public void updateB(String headline,String img,String content,String author,String source,String id){

        Db.update("update news_business  set headline='"+headline+"' , img='"+img+"',content='"+content+"',author='"+author+"',source='"+source+"' where id="+id+"");
    }

    //根据id删除
    public void delete_B(String id){

        Db.update("delete FROM news_business WHERE id = "+id+" ");
    }

    /**
     * 查询贷款攻略表
     * @return
     */
    public List<NewsStrategy> findS(){

        return NS.find("SELECT * FROM news_strategy order by rand() limit 5");
    }
    //根据id查询
    public NewsStrategy findSid(int i){

        return NS.findFirst("SELECT * FROM news_strategy WHERE id="+i+"");
    }
    //随机查询4条
    public List<NewsStrategy> findS_4(){

        return NS.find("SELECT * FROM news_strategy order by rand() limit 4");
    }
    //根据id倒序查询所有数据
    public List<NewsStrategy> findS_desc(){

        return NS.find("SELECT * FROM  news_strategy order by id desc");
    }
    //根据id查询单条数据不需循环
    public NewsStrategy findSids(String id){

        return NS.findFirst("SELECT * FROM news_strategy WHERE id="+id+"");
    }
    //根据id查询上下页
    public NewsStrategy findS_paging(int id,String size){

        return NS.findFirst("SELECT * FROM news_strategy WHERE id IN ( SELECT CASE WHEN SIGN (id - "+id+") > 0 THEN MIN(id) WHEN SIGN(id - "+id+") < 0 THEN MAX(id) END AS id FROM news_strategy WHERE id  "+size+" "+id+" GROUP BY SIGN(id - "+id+") ORDER BY SIGN(id - "+id+") ) ORDER BY id ASC;");
    }
    //更新表 标题 头图 内容 作者 来源
    public void updateS(String headline,String img,String content,String author,String source,String id){

        Db.update("update news_strategy  set headline='"+headline+"' , img='"+img+"',content='"+content+"',author='"+author+"',source='"+source+"' where id="+id+"");
    }
    //根据id删除
    public void delete_S(String id){

        Db.update("delete FROM news_strategy WHERE id = "+id+" ");
    }

    /**
     * 查询公告栏
     */
    //倒序查询
    public List<NewsBulletin>findBU_desc(){

        return NBU.find("SELECT * FROM  news_bulletin order by id desc");
    }
    //随机查询5条数据
    public List<NewsBulletin> findBU(){

        return NBU.find("SELECT * FROM news_bulletin order by rand() limit 5");
    }
    //根据id查询单条数据不需循环
    public NewsBulletin findBUids(String id){

        return NBU.findFirst("SELECT * FROM news_bulletin WHERE id="+id+"");
    }
    //根据id查询需循环
    public NewsBulletin findBUfor(String id){

        return NBU.findFirst("SELECT * FROM news_bulletin WHERE id="+id+"");
    }
    //根据id查询上下页
    public NewsBulletin findBU_paging(int id,String size){

        return NBU.findFirst("SELECT * FROM news_bulletin WHERE id IN ( SELECT CASE WHEN SIGN (id - "+id+") > 0 THEN MIN(id) WHEN SIGN(id - "+id+") < 0 THEN MAX(id) END AS id FROM news_bulletin WHERE id "+size+" "+id+" GROUP BY SIGN(id - "+id+") ORDER BY SIGN(id - "+id+") ) ORDER BY id ASC;");
    }
    //更新表 标题 头图 内容 作者 来源
    public void updateBU(String headline,String img,String content,String author,String source,String id){

        Db.update("update news_bulletin  set headline='"+headline+"' , img='"+img+"',content='"+content+"',author='"+author+"',source='"+source+"' where id="+id+"");
    }
    //根据id删除
    public void delete_BU(String id){

        Db.update("delete FROM news_bulletin WHERE id = "+id+" ");
    }

    /**
     * 查询热点资讯
     */
    //随机查询10条数据
    public List<NewsHot> findH_10(){

        return NH.find("SELECT * FROM news_hot order by rand() limit 10");
    }

    //根据id查询上下页
    public NewsHot findH_paging(int id,String size){

        return NH.findFirst("SELECT * FROM news_hot WHERE id IN ( SELECT CASE WHEN SIGN (id - "+id+") > 0 THEN MIN(id) WHEN SIGN(id - "+id+") < 0 THEN MAX(id) END AS id FROM news_hot WHERE id "+size+" "+id+" GROUP BY SIGN(id - "+id+") ORDER BY SIGN(id - "+id+") ) ORDER BY id ASC;");
    }

    //根据id倒序查询
    public List<NewsHot> findH_desc(){

        return NH.find("SELECT * FROM  news_hot order by id desc");
    }
    //根据id查询单条数据
    public NewsHot findHids(String id){

        return NH.findFirst("SELECT * FROM news_hot WHERE id="+id+"");
    }
    //更新表 标题 头图 内容 作者 来源
    public void updateH(String headline,String content,String author,String source,String id){

        Db.update("update news_hot  set headline='"+headline+"' , content='"+content+"',author='"+author+"',source='"+source+"' where id="+id+"");
    }

    //根据id删除
    public void delete_H(String id){

        Db.update("delete FROM news_hot WHERE id = "+id+" ");
    }

}
